from functools import reduce

print(isinstance(2,int))

# 可变参数 元组 的拆装包
def sum(*args):
    print(args)
    sum=0
    for i in args:
        sum=sum+i
    print("sum=",sum)

sum()
sum(1)
sum(1,2,3)

# 可变+不可变
def sum(str,*args):
    print(str,args)

sum('yuhang',*(1,2,3)) #yuhang (1, 2, 3)


# 关键字参数
def test(**kwargs) :
    print(kwargs)

dict={'1':'xiaoming','2':'xiaohong'}
test(**dict) #{'1': 'xiaoming', '2': 'xiaohong'}

test(x=123,y=345) #{'x': 123, 'y': 345}


# 参数默认值
def test(x,y=1,z=2):
    print(x,y,z)

test(1) #1 1 2
test(1,4) #1 4 2
test(1,z=5) #1 1 5


# 返回值
def add(a,b):
    return a+b

sum=add(1,2)
print(sum)


# 两个返回值 结果放在元组中
def test(x,y):
    return x,y

result=test(1,2)
print(result) #(1, 2)


# 全局变量
name='yuhang'
def test():
    global name #在函数内部修改不可变全局变量需要引入global
    name='liming'



# 内置函数
a=1
def test():
    b=2
    def inner_test():
        nonlocal b #修改父函数中局部变量
        global a #修改全局变量
        a+=1
        b+=1
    inner_test()
    print(a,b) #2 3
test()
print(a) #2


# 闭包
def fun(a,b):
    def inner_fun():
        print (a,b)
    return inner_fun

f1=fun(1,2) #1 2
f2=fun(3,4) #3 4

f1()
f2()


# 匿名函数
add = lambda a,b: a+b
result=add(1,2)
print(result) #3


l1=[{'a':1,'b':1},{'a':2,'b':1},{'a':3,'b':1},{'a':4,'b':1}]
result=max(l1,key=lambda x:x['a'])
print(result) #{'a': 4, 'b': 1}


# map
l1=[1,2,3,4]
l2=list(map(lambda x : x+2,l1))
print(l2) #[3, 4, 5, 6]

# reduce
tuple1=(1,2,3,4)
result= reduce(lambda a,b : a+b,tuple1)
print(result) #10


l1=[1,2,3,4,5,6]
l2=list(filter(lambda x:x>3,l1))
print(l2) #[4, 5, 6]


l1=[{'name':'yuhang',"age":10},{'name':'yuhang',"age":12},{'name':'yuhang',"age":11}]
l2=sorted(l1,key=lambda x : x['age'])
print(l2) #[{'name': 'yuhang', 'age': 10}, {'name': 'yuhang', 'age': 11}, {'name': 'yuhang', 'age': 12}]



